<?php
/**
 * Description: excel文件的模型
 * User: wuhao
 * Date: 2017-06-05
 * Time: 17:08
 */
namespace app\backend\model;

use think\Model;

class ExcelFile extends Model
{
    private $_file = 'z_file';
    private $_propDetail = 'z_prop_detail';
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增文件信息
     * @date   2017-06-05
     * @author wuhao
     *
     * @param  string  $name           文件名
     * @param  int     $mid            创建人id
     * @param  string  $url            文件地址
     * @param  string  $createTime     创建时间
     * @param  int     $record         总记录数
     * @param  int     $type           文件类型 1=用户 2=水电 3=房源 4=租赁合同 5=通联对账文件
     * @param  int     $handleStatus   生成对应的审核列表  0未生成 1正在处理中 2直接生成账单 3人工审核  4无需审核
     * @return int
     */
    public function addFile($name, $mid, $url, $createTime, $record, $type = 1, $handleStatus = 0)
    {
        if (!$name || !$url || !$record || !$type) {
            return false;
        }

        $data  = [
            'name'             => $name,
            'mid'              => $mid,
            'url'              => $url,
            'type'             => $type,
            'record'           => $record,
            'create_time'      => $createTime,
            'handle_status'    => $handleStatus,
        ];
        return $this->setTable($this->_file)->insertGetId($data);
    }

    /**
     * 获取上传文件信息
     * @date   2017-06-06
     * @author wuhao
     *
     * @param  int  $id   文件id
     * @return array
     */
    public function getFile($id)
    {
        if (!$id) {
            return false;
        }
        return $this->table($this->_file)->find($id);
    }

    /**
     * 批量插入物业水电管理费用列表
     * @date   2017-06-06
     * @author wuhao
     *
     * @param  array  $data   数据
     * @return array
     */
    public function insertPropFee($data)
    {
        if (!$data || !is_array($data)) {
            return false;
        }
        return $this->table($this->_propDetail)->insertAll($data);
    }

    /**
     * 根据文件id获取审批列表
     * @date   2017-06-06
     * @author wuhao
     *
     * @param  int  $fileId   文件Id
     * @param  string  $field 获取的字段
     * @return array
     */
    public function getPropByFileId($fileId, $field = '*')
    {
        if (!$fileId) {
            return false;
        }
        $where = [
            'file_id' => $fileId,
        ];
        $res = $this->table($this->_propDetail)->field($field)->where($where)->select();
        return $res ?: [];
    }

    /**
     * 更新审批列表状态
     * @date   2017-06-06
     * @author wuhao
     *
     * @param  array  $where   条件
     * @param  array  $data  数据
     * @return bool
     */
    public function updateProp($where, $data)
    {
        if (!$where || !$data) {
            return false;
        }
        return $this->table($this->_propDetail)->where($where)->update($data);
    }

    /**
     * 更新文件的信息
     * @date   2017-06-06
     * @author wuhao
     *
     * @param  int    $fileId   文件id
     * @param  array  $data     数据
     * @return bool
     */
    public function updateFile($fileId, $data)
    {
        if (!$fileId || !$data) {
            return false;
        }
        $where = ['id' => $fileId];
        return $this->table($this->_file)->where($where)->update($data);
    }

    /**
     * 获取对应的详细的数据
     * @date   2017-06-05
     * @author wuhao
     *
     * @param int       $mid           用户id
     * @param string    $begin         上传开始时间
     * @param string    $end           上传截止时间
     * @param int       $type          文件类型 1=用户 2=水电 3=房源 4=租赁合同
     * @param int       $verifyStatus  验证结果  是否验证中  -1全部 1=是 0=否
     * @param int       $status        文件数据是否正确  文件数据是否都是正确的 -1全部 0=否 1=是
     * @param int       $page          页数
     * @param int       $pageSize      每页个数
     * @return array
     */
    public function getFileList($mid, $begin, $end, $type = 1, $verifyStatus = -1, $status = -1, $page = 1, $pageSize = 15)
    {
        if ($mid) {
            $where['mid'] = $mid;
        }

        $where['type'] = $type;
        if ($begin && $end) {
            $end = date('Y-m-d', strtotime($end .'+1day'));  //between and  的后一个参数到 年月日 00:00:00
            $where['create_time'] = ['between', [$begin, $end]];
        }
        if ($verifyStatus != -1) {
            $where['verify_status'] = $verifyStatus;
        }
        if ($status != -1) {
            $where['status'] = $status;
        }
        if ($type == 2) {
            $where['handle_status'] = 0;
        }

        $res = $this->table($this->_file)->where($where)->page($page, $pageSize)->select();
        if ($res) {
            $sum = $this->table($this->_file)->where($where)->count();
            $total = ceil($sum / $pageSize);
            return [
                'list' => $res,
                'total' => $total,
            ];
        } else {
            return [];
        }
    }

    /**
     * 删除上传的错误的文件记录
     * @date   2017-06-07
     * @author wuhao
     *
     * @param array    $idArr    数组
     * @return bool
     */
    public function deleteWrongModelFile($idArr)
    {
        if (!$idArr) {
            return false;
        }
        $where = [
            'id' => ['in', $idArr],
            'status' => 0,//文件错误的
        ];
        return $this->table($this->_file)->where($where)->delete();
    }

    public function getDeleteFile($begin, $end, $page, $pageSize)
    {
        if (!$begin || !$end) {
            return false;
        }
        $where = [
            'status' => 0,//文件错误的
            'create_time' => [['egt', $begin], ['lt', $end]],
        ];

        $res = $this->table($this->_file)->field('id, url')->where($where)->page($page, $pageSize)->select();
        return $res;
    }

    /**
     * 获取审批列表
     * @date   2017-06-06
     * @author wuhao
     *
     * @param int       $buildId       楼宇id
     * @param int       $memberId      用户id
     * @param int       $feeType       费用类型 -1全部 3水，4电, 5公摊水，6公摊电,  8其他费用
     * @param int       $page          页数
     * @param int       $pageSize      每页个数
     * @return array
     */
    public function getProp($buildId = -1, $memberId, $feeType = -1, $page = 1, $pageSize = 15)
    {
        if (!$buildId  || !$memberId || !in_array($feeType, [-1,3,4,5,6,8]) || !$page || !$pageSize) {
            return false;
        }
        $where = [];
        if ($buildId != -1) {
            $where['building_id'] = $buildId;
        } else {
            $buildIdArr = $this->table('z_building')->field('id')->where(['mid' => $memberId, 'is_delete'=>0])->select();
            if ($buildIdArr) {
                $buildIdArr = array_column($buildIdArr, 'id');
                $where['building_id'] = ['in', $buildIdArr];
            } else {
                return [];
            }
        }
        if ($feeType != -1) {
            $where['fee_type'] = $feeType;
        }
        $where['status'] = 0;//未审批的

        $field = 'id, mid, building_id, number, last_water, this_water, actual_water, last_power, this_power, actual_power,
         room, begin_cycle, end_cycle, fee_name, fee_type, fee, create_time';
        $res = $this->table($this->_propDetail)->field($field)->where($where)->page($page, $pageSize)->select();
        if ($res) {
            $sum = $this->table($this->_propDetail)->where($where)->count();
            $total = ceil($sum / $pageSize);
            return [
                'list' => $res,
                'total' => $total
            ];
        } else {
            return [];
        }
    }

    /**
     * 根据id或id数组获取审批列表数据
     * @date   2017-06-06
     * @author wuhao
     *
     * @param  int|array  $id     审批id
     * @param  int        $status  审批状态 0=未审批
     * @param  string     $field 获取的字段
     * @return array
     */
    public function getPropById($id, $status = 0, $field = '*')
    {
        if (!$id || (!is_numeric($id) && !is_array($id)) || !$field) {
            return false;
        }
        $this->table($this->_propDetail)->field($field);
        if (is_array($id)) {
            $where = ['id' => ['in', $id], 'status' =>$status];
            $res = $this->where($where)->select();
        } else {
            $where = ['id' => $id, 'status'=> $status];
            $res = $this->where($where)->find();
        }
        return $res ?: [];
    }
}